package com.zueb.web.servlet;

import com.zueb.pojo.Checkin;
import com.zueb.service.CheckinService;
import com.zueb.service.impl.CheckinServiceImpl;
import com.zueb.util.ResponseWriterUtils;
import com.zueb.util.TimeUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "CheckinServlet", value = "/CheckinServlet")
public class CheckinServlet extends HttpServlet {
    private CheckinService checkinService = new CheckinServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转发请求到doPost
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转码
        request.setCharacterEncoding("UTF-8");
        //action表示用来区分是什么操作
        String action = request.getParameter("action");

        if (action.equals("addCheckin")) {
            addCheckin(request, response);  //插入信息
        } else if (action.equals("showCheckin")) {
            showCheckin(request, response);
        } else if (action.equals("showCheckinByNumber")) {
            showCheckinByNumber(request, response);
        }
    }

    /**
     * 学生查看自己的打卡记录
     * @param request
     * @param response
     */
    private void showCheckinByNumber(HttpServletRequest request, HttpServletResponse response) {
        //从页面获取用户信息
        Integer curPage = Integer.parseInt(request.getParameter("curPage"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String date = request.getParameter("date");
        String userNumber = request.getParameter("userNumber");
        //调用方法获取数据库数据
        List<Checkin> checkins = checkinService.showCheckinByNumber(userNumber, curPage, pageSize, date);
        int count = checkinService.showCheckinByNumberCount(userNumber, date);
        Map<String, Object> map = new HashMap<>();
        map.put("checkins", checkins);
        map.put("count", count);

        ResponseWriterUtils.writer(map, response);
    }

    /**
     * 管理员端查
     *
     * @param request
     * @param response
     * @throws IOException
     */
    private void showCheckin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //从页面获取用户信息
        int curPage = Integer.parseInt(request.getParameter("curPage"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        int facultyId = Integer.parseInt(request.getParameter("facultyId"));
        int classId = Integer.parseInt(request.getParameter("classId"));
        String date = request.getParameter("date");

        List<Checkin> checkins = checkinService.showCheckin(date, facultyId, classId, curPage, pageSize);
        int count = checkinService.showCheckinCount(date, facultyId, classId);

        Map<String, Object> map = new HashMap<>();
        map.put("checkins", checkins);
        map.put("count", count);

        ResponseWriterUtils.writer(map, response);
    }


    /**
     * 打卡功能
     *
     * @param request
     * @param response
     * @throws IOException
     */
    private void addCheckin(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //从前端获取数据
        String number = request.getParameter("number");
        String role = request.getParameter("role");
        String phone = request.getParameter("phone");
        String identification = request.getParameter("identification");
        String symptom = request.getParameter("symptom");
        String closer = request.getParameter("closer");
        String risk = request.getParameter("risk");
        String isolate = request.getParameter("isolate");
        String date = TimeUtil.getDate();
        String address = request.getParameter("address");
        //封装数据
        Checkin checkin = new Checkin();
        checkin.setAddress(address);
        checkin.setDate(date);
        checkin.setUserNumber(number);
        checkin.setRole(role);
        checkin.setPhone(phone);
        checkin.setIdentification(identification);
        checkin.setSymptom(symptom);
        checkin.setCloser(closer);
        checkin.setRisk(risk);
        checkin.setIsolate(isolate);

        //调用方法
        int add = checkinService.addCheckin(checkin);

        //返回参数
        if (add != 0) {
            response.getWriter().write("true");
        } else {
            response.getWriter().write("false");
        }

    }
}
